import {
  getSetting,
  chooseAddress,
  openSetting,
  showModel,
  showToast
} from '../../utils/asyncWx'
import regeneratorRuntime from '../../lib/runtime/runtime'
import {
  request
} from '../../request/index'
Page({

  data: {
    address: {},
    cart: [],
    allChecked: false,
    totalPrice: 0,
    totalNum: 0
  },
  onShow() {
    const address = wx.getStorageSync("address");

    let cart = wx.getStorageSync("cart") || [];

    //过滤后得购物车数组
    cart = cart.filter(v=>v.checked)

    this.setData({
      address
    })

    let totalPrice = 0
    let totalNum = 0

    cart.forEach(v => {
        totalPrice += v.num * v.goods_price
        totalNum += v.num
    })
    this.setData({
      cart,
      totalPrice,
      totalNum,
      address
    })
  },
  //点击支付
  async handleOrderPay(){
    //判断缓存中有没有  token

    const token = wx.getStorageSync("token");

    if(!token){
      wx.navigateTo({
        url: '/pages/auth/index',
        fail: ()=>{},
        complete: ()=>{}
      });
    }
    //请求头参数
    const header = {Authorization:token}

    const order_price = this.data.totalPrice
    const consignee_addr = this.data.address

    const cart = this.data.cart

    let goods = []
    cart.forEach(v=>goods.push({
      goods_id:v.goods_id,
      goods_number:v.num,
      goods_price:v.goods_price

    }))

    const orderParams = {order_price,consignee_addr,goods}

    //发送请求
    const res = await request({
      url:"/my/orders/create",method:"POST",data:orderParams,header
    })

    console.log(res);
  }

})